package com.test.map;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.test.game.tiles.EmptyTile;
import com.test.game.tiles.HexagonalTile;

public class MapReader {

   List<List<HexagonalTile>> matrix = new ArrayList<List<HexagonalTile>>();
   
   public static void main(String[] args) {
      MapReader mapReader = new MapReader();
      mapReader.generateMap("C:\\tmp\\data.txt");
   }
   
   
   public List<List<HexagonalTile>> generateMap(String filename) {
      this.read(filename);
      this.buildMap();
      return matrix;
   }
   
   private void read(String filename) {
      BufferedReader file = null;
      String line = null;
      List<HexagonalTile> row = null;
      
      try {
         file = new BufferedReader(new FileReader(filename));

         while((line = file.readLine()) != null) {
            row = new ArrayList<HexagonalTile>();
            while(line.length() > 2) {
               row.add(HexagonalTile.createTile(line.substring(0, 3)));
               line = line.substring(3);
            }
            matrix.add(row);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   private void buildMap() {
      for (int i = 0; i < matrix.size(); i++) {
         linkRows(i);
      }
      System.out.println("Completed!");
   }
   
   private void linkRows(int pos) {
      HexagonalTile up = null;
      HexagonalTile right = null;
      HexagonalTile bottom = null;
      List<HexagonalTile> firstRow = null;
      List<HexagonalTile> secondRow = null;
      List<HexagonalTile> thirdRow = null;
      boolean nextToLastRow = false;
      boolean lastRow = false;
      
      firstRow = matrix.get(pos);
      if((pos + 2 > matrix.size())) {
         lastRow = true;
      }
      else {
         secondRow = matrix.get(pos + 1);
         if((pos + 3 > matrix.size())) {
            nextToLastRow = true;
         }
         else {
            thirdRow = matrix.get(pos + 2);
         }
      }
      
      for (int i = 0; i < firstRow.size() - 1; i++) {
         up = firstRow.get(i);
         right = (lastRow ? null : secondRow.get(i + 1));
         bottom = (nextToLastRow || lastRow ? null : thirdRow.get(i));
      }
      
      linkHexes(up, bottom, right);
   }
   
   
   private void linkHexes(HexagonalTile up, HexagonalTile bottom, HexagonalTile right) {
      if (!up.isEmptyTile()) {
         if (right != null) {
            up.setBottomRightHex(right);
            right.setUpLeftHex(up);
            if (bottom != null) {
               up.setBottomHex(bottom);
               bottom.setUpHex(up);
               right.setBottomLeftHex(bottom);
               bottom.setUpRightHex(right);
            }
         } else if (bottom != null) {
            up.setBottomHex(bottom);
            bottom.setUpHex(up);
         }
      } else if (right != null) {
         if (bottom != null) {            
            right.setBottomLeftHex(bottom);
            bottom.setUpRightHex(right);
         }
      }
   }
}
